<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/11
 * Time: 13:02
 */

namespace app\common\controller;
use Firebase\JWT\JWT;

class Token{
    //md5加密(经理名)
    static $key = '4dda4b66a63a2f7695deaf2a84857bcc';

    /**
     * data 需要存储的数据
     * exp 过期时间(秒)
     */
    static function encode(array $data,int $exp){
        $nowTime = time();
        $token = [
            'data' => $data,
            'sub' => $nowTime,
            'exp' => $nowTime + $exp
        ];
        $jwt = JWT::encode($token,static::$key,'HS256');
        return $jwt;
    }

    static function decode(string $jwt){
        try{
            $decoded = JWT::decode($jwt,static::$key,array('HS256'));
            return [
                'status' => true,
                'content' => $decoded->data
            ];
        }catch(\UnexpectedValueException $e) {
            return [
                'status' => false,
                'content' => $e->getMessage()
            ];
        }
    }
}